package service

import (
	context3 "ksd-social-api/modules/msg/model/context"
	cmodel "ksd-social-api/modules/special/model"
)

/**
 * 用户购买专栏
 * @author feige
 * @date 2023-11-23
 * @version 1.0
 * @desc
 */
type UserSpecialService struct {
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  保存用户购买专栏信息
 */
func (service *UserSpecialService) SaveUserSpecial(
	OutTradeNo string, TransactionId string, orderJson string, payMethod int, payMethodName string, price string,
	userId uint64, opid uint64, systemId uint64, systemName string) bool {
	// 1： 判断用户是否购买此课程，如果购买过了就就不要去保存订单
	isBuyFlag := userBuySpecialMapper.CountUserBuySpecialNo(userId, opid, systemId)
	if isBuyFlag == 0 {
		userBuySpecial := cmodel.UserBuySpecial{}
		// 根据课程id查询课程信息
		special, _ := specialMapper.GetById(opid, systemId)
		userVo := userMapper.GetUserByID(userId, systemId)

		userBuySpecial.SystemId = systemId
		userBuySpecial.SystemName = systemName
		// 处理返回结果
		userBuySpecial.Opid = opid
		userBuySpecial.Title = special.Title
		userBuySpecial.Cover = special.Img

		userBuySpecial.UserId = userId
		userBuySpecial.Username = userVo.Username
		userBuySpecial.Avatar = userVo.Avatar
		userBuySpecial.Uuid = userVo.Uuid
		userBuySpecial.Phone = userVo.Telephone
		userBuySpecial.Address = userVo.Address
		userBuySpecial.Nickname = userVo.Nickname

		userBuySpecial.Code = ""
		userBuySpecial.PayMethod = payMethod
		userBuySpecial.PayMethodName = payMethodName
		userBuySpecial.Tradeno = OutTradeNo
		userBuySpecial.Orderno = TransactionId
		userBuySpecial.OrderJson = orderJson
		userBuySpecial.Price = special.Price
		userBuySpecialMapper.SaveUserBuySpecial(userBuySpecial)

		// 保存默认消息
		messagePointService.SaveMessagePonitDefault(userId, systemId, userVo.Uuid)
		// 注册消息发送---rabbitmq 100ms
		messageMeContext := context3.MessageMeContext{}
		messageMeContext.UserId = userId
		messageMeContext.Title = special.Title
		messageMeContext.Uuid = userVo.Uuid
		messageMeContext.SystemId = systemId
		messageMeContext.SystemName = "系统512"
		messageMeContext.Username = userVo.Nickname
		messageMeContext.Url = "https://www.study512.com/special/" + special.Path
		messageMeService.SaveMessageMeBuySpecial(&messageMeContext)
		return true
	}
	return false
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  判断用户是否购买此专栏
 */
func (service *UserSpecialService) CountUserBuySpecial(userId uint64, specialId uint64, systemId uint64) bool {
	// 1： 判断用户是否购买此专栏，如果购买过了就就不要去保存订单
	isBuyFlag := userBuySpecialMapper.CountUserBuySpecialNo(userId, specialId, systemId)
	return isBuyFlag == 0
}
